package com.code;

import java.sql.ResultSet;
import java.util.ArrayList;

import com.Global.Global;
import com.example.R;
import com.object.obTongMatHang;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class TK_TongLoiNhuan extends Activity {

	private String strNhomChon = "";
	private String strKhoChon = "";
	ArrayList<Integer> date_actual;
	private String date;
	private LinearLayout llMainTongLoiNhuan;
	private TableRow rowFocus;
	private Integer choose_tmh = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tongloinhuan);

		llMainTongLoiNhuan = (LinearLayout) findViewById(R.id.lltongloinhuan);
		strNhomChon = super.getIntent().getExtras().getString("strNhomChon");
		strKhoChon = super.getIntent().getExtras().getString("strKhoChon");
		date_actual = super.getIntent().getExtras()
				.getIntegerArrayList("date_tk_tln");
		date = String.valueOf(date_actual.get(2))
				+ DetailCuocGoi.setMonth(date_actual.get(1))
				+ Global.formatDay(date_actual.get(0));
		choose_tmh = super.getIntent().getExtras().getInt("chose_tln");
		loadData(choose_tmh);
	}

	public void loadData(int choose) {

		Float totalThanhTien = 0.f;
		Float totalVon = 0.0f;
		Float totalLai = 0.0f;
		Integer totalsoLuongXuat = 0;

		StringBuffer sql = new StringBuffer(
				" SELECT Products.[Product ID] as ProductID, ");
		sql.append(" sum(vattuxuat.SL) as SL, ");
		sql.append(" SUM(vattuxuat.SL * vattuxuat.dongia) AS DoanhSo ,SUM(vattuxuat.SL *"
				+ " CASE WHEN ISNULL(vattuxuat.Von_Cost,'') = '' THEN 0 ELSE vattuxuat.Von_Cost END) AS VON,");
		sql.append(" (SUM(vattuxuat.SL * vattuxuat.dongia) - SUM(vattuxuat.SL * "
				+ " CASE WHEN ISNULL(vattuxuat.Von_Cost,'') = '' THEN 0 ELSE vattuxuat.Von_Cost END)) AS LAI ");
		sql.append(" FROM Phieuxuat INNER JOIN vattuxuat ON Phieuxuat.SoCt = vattuxuat.SoCT INNER JOIN ");
		sql.append(" Products ON vattuxuat.[Product ID] = Products.[Product ID] ");
		if (!strKhoChon.equals(""))
			sql.append(" AND Phieuxuat.MSK COLLATE DATABASE_DEFAULT in (select value from dbo.fn_Split('"
					+ strKhoChon + "',','))");
//		if (choose == TK_ChonMau.CHOOSE_NAM)
			sql.append(" AND DATEPART(YYYY,Phieuxuat.Ngay) = DATEPART(YYYY,'"
					+ date + "')");
		if (choose == TK_ChonMau.CHOOSE_QUI)
			sql.append(" AND DATEPART(quarter,Phieuxuat.Ngay) = DATEPART(quarter,'"
					+ date + "')");
		else if (choose == TK_ChonMau.CHOOSE_THANG)
			sql.append(" AND DATEPART(MM,Phieuxuat.Ngay) = DATEPART(MM,'"
					+ date + "')");
		else if (choose == TK_ChonMau.CHOOSE_TUAN)
			sql.append(" AND DATEPART(WEEK,Phieuxuat.Ngay) = DATEPART(WEEK,'"
					+ date + "')");
		else if (choose == TK_ChonMau.CHOOSE_NGAY)
			sql.append(" AND CAST(Phieuxuat.Ngay as date) = CAST('" + date
					+ "' as date) ");
		if (!strNhomChon.equals(""))
			sql.append(" AND Products.[Category ID] =  '" + strNhomChon + "'");
		sql.append(" GROUP BY Products.[Product ID] ");
		ResultSet rs = Global.excuteQuerySQLServer(sql.toString());

		final TableLayout table = new TableLayout(this);
		llMainTongLoiNhuan.addView(table);
		TableRow trHead = new TableRow(this);
		trHead.setWeightSum(6.0f);

		TextView tvMaHang = new TextView(this);
		tvMaHang.setText("Mã hàng");
		TK_TongMatHang.setLayoutForTextView(tvMaHang, true, Gravity.CENTER, 10,
				2.0f);

		TextView tvSLXuat = new TextView(this);
		tvSLXuat.setText("SLX");
		TK_TongMatHang.setLayoutForTextView(tvSLXuat, true, Gravity.CENTER, 10,
				0.5f);

		TextView tvThanhTien = new TextView(this);
		tvThanhTien.setText("Doanh Số");
		TK_TongMatHang.setLayoutForTextView(tvThanhTien, true, Gravity.CENTER,
				10, 1.5f);

		TextView tvVOn = new TextView(this);
		tvVOn.setText("Vốn");
		TK_TongMatHang.setLayoutForTextView(tvVOn, true, Gravity.CENTER, 10,
				1.0f);

		TextView tvLai = new TextView(this);
		tvLai.setText("Lãi");
		TK_TongMatHang.setLayoutForTextView(tvLai, true, Gravity.CENTER, 10,
				1.0f, true);
		trHead.addView(tvMaHang);
		trHead.addView(tvSLXuat);
		trHead.addView(tvThanhTien);
		trHead.addView(tvVOn);
		trHead.addView(tvLai);
		table.addView(trHead);
		try {
			while (rs.next()) {
				// set value for TongMatHang
				final obTongMatHang obTMH = new obTongMatHang();
				if (rs.getString("ProductID") != null)
					obTMH.setMaHang(rs.getString("ProductID"));
				if (rs.getInt("SL") != 0)
					obTMH.setSoLuongXuat(String.valueOf(rs.getInt("SL")));
				else
					obTMH.setSoLuongXuat("0");
				if (rs.getString("DoanhSo") != null)
					obTMH.setThanhTien(rs.getString("DoanhSo"));
				else
					obTMH.setThanhTien("0.0f");

				if (rs.getString("Von") != null)
					obTMH.setVon(rs.getString("Von"));
				else
					obTMH.setVon("0.0f");
				if (rs.getString("Lai") != null)
					obTMH.setLai(rs.getString("Lai"));
				else
					obTMH.setLai("0.0f");

				// add date in textview
				final TableRow trHeads = new TableRow(this);
				trHeads.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						try {
							if (rowFocus != null)
								rowFocus.setBackgroundColor(Color.TRANSPARENT);
							rowFocus = (TableRow) v;
							v.setBackgroundColor(Color.YELLOW);
						} catch (Exception e) {
						}
					}
				});
				trHeads.setOnLongClickListener(new OnLongClickListener() {
					public boolean onLongClick(View v) {
						// if (!obTMH.getMaHang().equals("")) {
						// Intent i = new Intent(TK_TongLoiNhuan.this,
						// DetailTongMatHang.class);
						// Bundle b = new Bundle();
						// b.putString("strNhomChonDetail", strNhomChon);
						// b.putString("strKhoChonDetail", strKhoChon);
						// b.putString("maHangDetail", obTMH.getMaHang());
						// b.putIntegerArrayList("dateDetail", date_actual);
						// i.putExtras(b);
						// startActivityForResult(i, REQUESTCODE_DETAIL);
						// }
						return false;
					}
				});
				trHeads.setWeightSum(6.0f);

				TextView tvMaHangs = new TextView(this);
				tvMaHangs.setText(obTMH.getMaHang());
				TK_TongMatHang.setLayoutForTextView(tvMaHangs, false,
						Gravity.LEFT, 10, 2.0f);

				TextView tvSLXuats = new TextView(this);
				tvSLXuats.setText(obTMH.getSoLuongXuat());
				TK_TongMatHang.setLayoutForTextView(tvSLXuats, false,
						Gravity.CENTER, 10, 0.5f);

				TextView tvThanhTiens = new TextView(this);
				tvThanhTiens.setText(Global.formatDecimal(Float.valueOf(obTMH
						.getThanhTien())));
				TK_TongMatHang.setLayoutForTextView(tvThanhTiens, false,
						Gravity.RIGHT, 10, 1.5f);

				TextView tvVons = new TextView(this);
				tvVons.setText(Global.formatDecimal(Float.valueOf(obTMH
						.getVon())));
				TK_TongMatHang.setLayoutForTextView(tvVons, false,
						Gravity.RIGHT, 10, 1.0f);

				TextView tvLais = new TextView(this);
				tvLais.setText(Global.formatDecimal(Float.valueOf(obTMH
						.getLai())));
				TK_TongMatHang.setLayoutForTextView(tvLais, false,
						Gravity.RIGHT, 10, 1.0f, true);

				trHeads.addView(tvMaHangs);
				trHeads.addView(tvSLXuats);
				trHeads.addView(tvThanhTiens);
				trHeads.addView(tvVons);
				trHeads.addView(tvLais);
				// trHeads.addView(tvTons);
				table.addView(trHeads);

				totalsoLuongXuat = totalsoLuongXuat
						+ Integer.valueOf(obTMH.getSoLuongXuat());
				totalThanhTien = totalThanhTien
						+ (Float.valueOf(obTMH.getThanhTien()));
				totalVon = totalVon + (Float.valueOf(obTMH.getVon()));
				totalLai = totalLai + Float.valueOf(obTMH.getLai());
			}
			// add date in textview
			TableRow trHeadEnd = new TableRow(this);
			trHeadEnd.setWeightSum(6.0f);

			TextView tvMaHangEnd = new TextView(this);
			tvMaHangEnd.setText("Tổng");
			TK_TongMatHang.setLayoutForTextView(tvMaHangEnd, false,
					Gravity.CENTER, 10, 2.0f);
			tvMaHangEnd.setBackgroundResource(R.layout.border_topleftbottom);

			TextView tvSLXuatEnd = new TextView(this);
			tvSLXuatEnd.setText(totalsoLuongXuat.toString());
			TK_TongMatHang.setLayoutForTextView(tvSLXuatEnd, false,
					Gravity.CENTER, 10, 0.5f);
			tvSLXuatEnd.setBackgroundResource(R.layout.border_topleftbottom);

			TextView tvThanhTienEnd = new TextView(this);
			tvThanhTienEnd.setText(Global.formatDecimal(totalThanhTien)
					.toString());
			TK_TongMatHang.setLayoutForTextView(tvThanhTienEnd, false,
					Gravity.CENTER, 10, 1.5f);
			tvThanhTienEnd.setBackgroundResource(R.layout.border_topleftbottom);
			tvThanhTienEnd.setGravity(Gravity.RIGHT);

			TextView tvVonEnd = new TextView(this);
			tvVonEnd.setText(Global.formatDecimal(totalVon).toString());
			TK_TongMatHang.setLayoutForTextView(tvVonEnd, false,
					Gravity.CENTER, 10, 1.0f);
			tvVonEnd.setBackgroundResource(R.layout.border_topleftbottom);
			tvVonEnd.setGravity(Gravity.RIGHT);

			TextView tvLaiEnd = new TextView(this);
			tvLaiEnd.setText(Global.formatDecimal(totalLai).toString());
			TK_TongMatHang.setLayoutForTextView(tvLaiEnd, false,
					Gravity.CENTER, 10, 1.0f, true);
			tvLaiEnd.setBackgroundResource(R.layout.border);
			tvLaiEnd.setGravity(Gravity.RIGHT);
			trHeadEnd.addView(tvMaHangEnd);
			trHeadEnd.addView(tvSLXuatEnd);
			trHeadEnd.addView(tvThanhTienEnd);
			trHeadEnd.addView(tvVonEnd);
			trHeadEnd.addView(tvLaiEnd);
			// trHeadEnd.addView(tvTonEnd);
			table.addView(trHeadEnd);

			rs.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}

	}
}
